/**
 * Created by JetBrains WebStorm.
 * User: yangn
 * Date: 11-12-19
 * Time: 下午1:42
 * for 省市联动
 */

(function(){
        $.fn.areaSelect=function(options){
            var settings={
                name:"",
                isInit:false,//是否默认展开
                initValue:{province:0,city:0,area:0}
            };
            var $this=$(this);
            var $selectSS,$selectXQ;
            var $op=$("<option></option>");
            var ls=0,lq=0;
            var opts=$.extend(settings,options);
            var name=opts.name?opts.name:"";
            var isInit=opts.isInit;
            var initValue=opts.initValue;

            $selectSS=$this.nextAll("select.ss").length>0?$this.nextAll("select.ss").eq(0):$("<select class='ss'></select>");
            $selectXQ=$this.nextAll("select.xq").length>0?$this.nextAll("select.xq").eq(0):$("<select class='xq'></select>");

            $this.attr("name","province"+name);
            $selectSS.attr("name","city"+name);
            $selectXQ.attr("name","area"+name);

            $selectSS.insertAfter($this);
            $selectXQ.insertAfter($selectSS);
            showOrHide($selectSS);
            showOrHide($selectXQ);
             if(isInit && initValue){
                 changeSQ(initValue.province,0,initValue);
            }else{
                 changeSQ(0,0);
             }
            function changeSQ(id,index,initValue){
                $selectXQ.html("");
                if(id!=0){
                   $.getJSON("../common/servicer/js/common/"+id+".json",function(data){
                        if(data){
                            $selectSS.html("");
                            ls=data.length;
                            $selectSS.attr("id",data[0].id);
                            for(var i=0;i<ls;i++){
                                $op.clone().val(data[i].id).text(data[i].s).appendTo($selectSS);
                            }
                            $selectSS.insertAfter($this);
                            if(initValue){
                                var p=$this.find("option").index($this.find("option[value='"+initValue.province+"']"));
                                var c=$selectSS.find("option").index($selectSS.find("option[value='"+initValue.city+"']"));
                                if(data[c].x){
                                    $selectXQ.attr("id",data[c].x[0].n.id);
                                    lq=data[c].x.length;
                                    for(var j=0;j<lq;j++){
                                        $op.clone().val(data[c].x[j].id).text(data[c].x[j].n).appendTo($selectXQ);
                                    }

                                }
                                $selectSS[0].selectedIndex=c;
                                var a=$selectXQ.find("option").index($selectXQ.find("option[value='"+initValue.area+"']"));
                                $selectXQ[0].selectedIndex=a;
                                $this[0].selectedIndex=p;
                            }else{
                                if(data[index].x){
                                    $selectXQ.attr("id",data[index].x[0].n.id);
                                    lq=data[index].x.length;
                                    for(var j=0;j<lq;j++){
                                        $op.clone().val(data[index].x[j].id).text(data[index].x[j].n).appendTo($selectXQ);
                                    }
                                }
                                $selectSS[0].selectedIndex=index;
                            }
                            showOrHide($selectSS);
                            showOrHide($selectXQ);
                        }
                    });
                }else{
                    $selectSS.hide();
                    $selectXQ.hide();
                }
            }
            $this.change(function(){
                initValue=false;
                changeSQ($this.val(),0,initValue);
            });
            $selectSS.change(function(){
                 initValue=false;
                changeSQ($this.val(),this.selectedIndex,initValue);
            });
            function showOrHide(selectObj){
                if(selectObj.find("option").length>0){
                    selectObj.show();
                }else{
                    selectObj.hide();
                }
            }
        };
    })(jQuery);